<?php
	/**
	 * @author Evica Ilieva
	 * @date 2012-06-29
	 */
	class ArxivApiImplDAO implements RepositoryDAO{
		
		/**
		 * Get Document Object by doi
		 * No implementation for searchByDoi for this Digital Library
		 *
		 * @param String $doi
		 */
		public function searchByDoi($doi){
			return null;
		}
		
		/**
		 * Get Document Object by uid
		 *
		 * @param String $uid
		 */
		public function searchByUid($uid) {
			// Replace the constant with the actual arxiv_id
			$request_url = str_replace('[ARXIV_ID]', $uid, RepositoryConnectionProperty::$_url);
			RepositoryConnectionProperty::$_url = $request_url;
			//Connect to the repository
			$conn = new RepositoryConnection();
			
			//The connection returns a DOM from the URL for the HTML page and
			$xml=$conn->getConnection();
			
			$record = $xml->entry;
			
			$keyword_separator=";";
			$document = new Document();
			$metadata = new Metadata();
			$file = new File();
			$keywords = array();
			$tmp_pages = "";
			
			if(isset($record->title)){
				$metadata->setTitle(trim($record->title));
				$metadata->setTitleAscii(utf8_deaccent($record->title));
			}
			
			$journal_ref = array();
			$children = $record->children('http://arxiv.org/schemas/atom');
			if(isset($children->journal_ref))
				$metadata->setSecondaryTitle((string)$children->journal_ref);
			
			if(isset($children->doi))
				$metadata->setDoi((string)$children->doi);
			
			if(isset($record->published)){
				$pub_date = $record->published;
				$metadata->setYear(date("Y-m-d", strtotime($pub_date)));
			}
			
			if(isset($record->summary)){
				$metadata->setAbstract(trim($record->summary));
				$metadata->setAbstractAscii(utf8_deaccent($record->summary));
			}
			
			if(isset($record->author)){
				$authors = $record->author;
				
				if (!empty($authors)) {
					foreach ($authors as $author) {
						$author = $author->name;
						$name_array[] = str_replace(",", "", $author);
					}
				}
				
				if (isset($name_array)) $metadata->setAuthors(join (", ", $name_array));
			}
			
			if(isset($children->primary_category)){
				$category = $children->primary_category;
				$keyword_array = $category->attributes();
				
			}
			
			$keyword = new Keyword();
			$keyword->setKeyword(trim($keyword_array['term'][0]));
			$keywords_arr = array($keyword);
			$metadata->setKeywords($keywords_arr);
			
			$document->setUid("ARXIV:$uid");
			
			$metadata->setUrl("http://arxiv.org/abs/$uid");
			$document->setMetadata($metadata);
			$document->setFile($file);
			
			
			return $document;
		}
		
		public function findDoiByISSN($issn, $title){}
		
		public function searchByTitle($title){}
		
		
	}

?>